class TeamsController < ApplicationController
  skip_before_action :verify_authenticity_token
  def index
    @teams = Team.all
    render "index"
  end
  def show
    team_id = params[:id]
    @team = Team.find(team_id)
    @positions = Position.where("team_id = ?", params[:id])
  end
  def position
    position_id = params[:position_id]
    @position = Position.find(position_id)
    @employees  =  Employee.where("position_id = ?", position_id)
    @all_employees = []
    @all_teams = []
    all_employees = Employee.all
    all_teams = Team.all
    all_employees.each do |a_e|
      p_r = PositionRelationship.where("position_id = ? and entity_id = ? and relationship_type = 1 ", @position.id, a_e.id)
      e_r = Hash.new
      position = Position.find(a_e.position_id)
      team = Team.find(position.team_id)
      e_r["employee_name"] = a_e.name
      e_r["employee_id"] = a_e.id
      e_r["employee_position"] = team.name + "/" + position.name
      if p_r.empty?
        e_r["employee_select"] = 0
      else
        e_r["employee_select"] = 1
      end
      @all_employees.push(e_r)
    end
    all_teams.each do |a_t|
      p_r2 = PositionRelationship.where("position_id = ? and entity_id = ? and relationship_type = 2 ", @position.id, a_t.id)
      t_r = Hash.new
      t_r["team_name"] =  a_t.name
      t_r["team_id"] = a_t.id
      if p_r2.empty?
        t_r["team_select"] = 0
      else
        t_r["team_select"] = 1
      end
      @all_teams.push(t_r)
    end
    @licenses_team = License.where("license_type = 2 and position_id = ? and publish_status = 1", position_id)
    @licenses_employee = License.where("license_type = 1 and position_id = ? and publish_status = 1", position_id)
    render 'position'
  end
  def team_relation_save
    team_list = params[:team_selecteds]
    position_id = params[:position_id]
    PositionRelationship.where("position_id = ? and relationship_type = 2", position_id).delete_all
    team_list.each do |tl|
      position_relationship = PositionRelationship.new
      position_relationship.position_id = position_id
      position_relationship.relationship_type = 2
      position_relationship.entity_id = tl
      position_relationship.created_at = Time.now
      position_relationship.updated_at = Time.now
      position_relationship.save
    end
  end
  def employee_relation_save
    employee_list = params[:employee_selecteds]
    position_id = params[:position_id]
    PositionRelationship.where("position_id = ? and relationship_type = 1", position_id).delete_all
    employee_list.each do |el|
      position_relationship = PositionRelationship.new
      position_relationship.position_id = position_id
      position_relationship.relationship_type = 1
      position_relationship.entity_id = el
      position_relationship.created_at = Time.now
      position_relationship.updated_at = Time.now
      position_relationship.save
    end
  end
  def export_license
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => '青岛分公司一般中层密码条'
    sheet2 = book.create_worksheet :name => '青岛分公司一般中层部门密码条'
    sheet3 = book.create_worksheet :name => '青岛分公司一般员工密码条'
    sheet4 = book.create_worksheet :name => '青岛分公司一般员工部门密码条'
    line1 = 1
    line2 = 1
    line3 = 1
    line4 = 1
    teams = Team.all
    teams.each do |t|
      positions = Position.where("team_id = ?", t.id)
      if !(positions.empty?)

        positions.each do |p|
          if p.position_type == 2 || p.position_type == 1
            licenses1 = License.where("position_id = ? and license_type = 1 and publish_status = 1", p.id)
            if !(licenses1.empty?)
              sheet1[line1,0] = t.name + p.name
              line1 = line1 + 1
              licenses1.each do |l1|
                sheet1[line1,0] = "员工民主评议"
                sheet1[line1,1] = "密码"
                sheet1[line1,2] = "电脑端和手机端登录网址"
                sheet1[line1,3] = "二维码扫描登录"
                line1 = line1 + 1
                sheet1[line1,0] = t.name + p.name
                sheet1[line1,1] = l1.content
                sheet1[line1,2] = "http://cp.yzlma.com/:3001/"
                sheet1[line1,3] = "扫描现场二维码"
                line1 = line1 + 1
              end
            end
            licenses2 = License.where("position_id = ? and license_type = 2 and publish_status = 1", p.id)
            if !(licenses2.empty?)
              sheet2[line2,0] = t.name + p.name
              line2 = line2 + 1
              licenses2.each do |l2|
                sheet2[line2,0] = "部门民主评议"
                sheet2[line2,1] = "密码"
                sheet2[line2,2] = "电脑端和手机端登录网址"
                sheet2[line2,3] = "二维码扫描登录"
                line2 = line2 + 1
                sheet2[line2,0] = t.name + p.name
                sheet2[line2,1] = l2.content
                sheet2[line2,2] = "hhttp://cp.yzlma.com/:3001/"
                sheet2[line2,3] = "扫描现场二维码"
                line2 = line2 + 1
              end
            end
          elsif p.position_type == 3
            licenses1 = License.where("position_id = ? and license_type = 1 and publish_status = 1", p.id)
            if !(licenses1.empty?)
              sheet3[line3,0] = t.name + p.name
              line3 = line3 + 1
              licenses1.each do |l1|
                sheet3[line3,0] = "员工民主评议"
                sheet3[line3,1] = "密码"
                sheet3[line3,2] = "电脑端和手机端登录网址"
                sheet3[line3,3] = "二维码扫描登录"
                line3 = line3 + 1
                sheet3[line3,0] = t.name + p.name
                sheet3[line3,1] = l1.content
                sheet3[line3,2] = "http://cp.yzlma.com/:3001/"
                sheet3[line3,3] = "扫描现场二维码"
                line3 = line3 + 1
              end
            end
            licenses2 = License.where("position_id = ? and license_type = 2 and publish_status = 1", p.id)
            if !(licenses2.empty?)
              sheet4[line4,0] = t.name + p.name
              line4 = line4 + 1
              licenses2.each do |l2|
                sheet4[line4,0] = "部门民主评议"
                sheet4[line4,1] = "密码"
                sheet4[line4,2] = "电脑端和手机端登录网址"
                sheet4[line4,3] = "二维码扫描登录"
                line4 = line4 + 1
                sheet4[line4,0] = t.name + p.name
                sheet4[line4,1] = l2.content
                sheet4[line4,2] = "http://cp.yzlma.com/:3001/"
                sheet4[line4,3] = "扫描现场二维码"
                line4 = line4 + 1
              end
            end
          end  
          
        end
      end
    end
    book.write 'public/download/excel-file.xls'
    send_file "#{Rails.root}/public/download/excel-file.xls"
  end
  def export_team_relationship
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => '青岛分公司评分配置表'
    sheet1[1,0] = "部门名称"
    sheet1[1,1] = "岗位名称"
    sheet1[1,2] = "被评价员工"
    sheet1[1,3] = "被评价员工数量"
    sheet1[1,4] = "被评价部门"
    sheet1[1,5] = "被评价部门数量"
    line = 2
    teams = Team.all
    teams.each do |t|
      positions = Position.where("team_id = ?", t.id)
      if !(positions.empty?)
        positions.each do |p|
          employee_text = ""
          employee_num = 0
          team_text = ""
          team_num = 0
          relationships = PositionRelationship.where("position_id = ?", p.id)          
          relationships.each do |rl|            
            if rl.relationship_type == 1
              employee = Employee.find(rl.entity_id)
              employee_text = employee_text + ";" + employee.name
              employee_num = employee_num +1
            elsif rl.relationship_type == 2
              team = Team.find(rl.entity_id)
              team_text = team_text + ";" + team.name
              team_num = team_num + 1
            end
          end
          sheet1[line,0] = t.name
          sheet1[line,1] = p.name
          sheet1[line,2] = employee_text
          sheet1[line,3] = employee_num
          sheet1[line,4] = team_text
          sheet1[line,5] = team_num
          line = line+1
        end
      end
    end
    book.write 'public/download/评分配置表.xls'
    send_file "#{Rails.root}/public/download/评分配置表.xls"
  end
end
